package ufpb.compiladores.sintatico;

import java.util.ArrayList;

/**
 *
 * @author SemiNick
 */
public class Production {

    private ArrayList<Clause> clauses = new ArrayList<Clause>();
    private final Object lock = new Object();
    private final String name;
    private final boolean opt;
    
    public Production(String name, boolean opt) {
        
        if (name == null) {
            
            throw new NullPointerException("Production name cannot be null");
        } else if (name.equals("")) {
            
            throw new IllegalArgumentException("Production name cannot be empty");
        }
        
        this.name = name;
        this.opt = opt;
    }
    
    public final boolean isOptional() {
        
        return this.opt;
    }
    
    public final void addClause(Clause cla) {
        
        if (cla == null) {
            
            throw new NullPointerException("Clause to be added cannot be null");
        }
        
        synchronized (this.lock) {
            
            clauses.add(cla);
        }
    }
    
    public final void removeClause(Clause cla) {
        
        if (cla == null) {
            
            throw new NullPointerException("Clause to be removed cannot be null");
        }
        
        synchronized (this.lock) {
            
            this.clauses.remove(cla);
        }
    }
    
    public final String getName() {
        
        return this.name;
    }
    
    public Clause[] getClauses() {
        
        Clause[] ret = null;
        synchronized (this.lock) {
            
            ret = new Clause[this.clauses.size()];
            this.clauses.toArray(ret);
        }
        
        return ret;
    }
}
